package memory;

import java.io.File;
import java.io.IOException;
import java.net.URL;
 
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
 
 
 public class WavPlayer implements Runnable {    
 
     private SourceDataLine line = null;
     private AudioInputStream audioInputStream = null;
	 private AudioFormat audioFormat;
	 private boolean stop = false;
 
     public WavPlayer(URL fileURL) throws Exception{
         audioInputStream = AudioSystem.getAudioInputStream(fileURL); 
         audioFormat = audioInputStream.getFormat();

         DataLine.Info info = new DataLine.Info(
        		 SourceDataLine.class,
        		 audioFormat);
         
         line = (SourceDataLine) AudioSystem.getLine(info);
     }

	@Override
	public void run() {
		try {
			line.open(audioFormat);
		} catch (LineUnavailableException e) {}
		
		line.start();
		try {
 			byte bytes[] = new byte[1024];
 			int bytesRead=0;			
 			
 			while (((bytesRead = audioInputStream.read(bytes, 0, bytes.length)) != -1)
 					&& !stop) {
 				
 				line.write(bytes, 0, bytesRead);
 			}
 		} catch (IOException io) {}
 		line.close();
	}
 
 }